import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {HttpClient} from '@angular/common/http';

export class CommentSubject {
  title: string;
  content: string;
  rate: number;
  movieId: string;

  constructor(title: string = '', content: string = '', rate: number = 1, movieId: string = '') {
    this.title = title;
    this.content = content;
    this.rate = rate;
    this.movieId = movieId;
  }
}

@Component({
  selector: 'app-detail',
  templateUrl: './movie-detail.component.html',
  styleUrls: ['./movie-detail.component.css']
})
export class MovieDetailComponent implements OnInit {
  movie: object;
  movieTab = 0;
  isFavorite = false;
  rate = 3;
  rates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  realRate = this.rate;
  comments: any;
  commentSubject: CommentSubject = new CommentSubject();

  constructor(private http: HttpClient, private activatedRoute: ActivatedRoute) {
    const id = this.activatedRoute.snapshot.params['id'];
    if (id !== undefined && id !== '0') {
      this.http.get('/movie/detail/' + id).subscribe(result => {
        this.movie = result;
        this.rate = result['rate'];
        this.realRate = this.rate;
      });
      this.http.get(`/movie/comment/${id}/list?size=5`).subscribe(res => this.comments = res);
      this.checkIsFavorite(id);
      this.commentSubject.movieId = id;
    }
  }

  ngOnInit() {
  }

  checkIsFavorite(movieId: string) {
    this.http.get('/user/isfavorite/' + movieId).subscribe(res => this.isFavorite = res['success']);
  }


  favorite(movieId: string) {
    const uid = localStorage.getItem('uid');
    if (uid === null) {
      alert('请先登录');
      return;
    }
    const url = this.isFavorite ? '/user/unfavorite/' : '/user/favorite/';
    this.http.get(url + movieId).subscribe(res => {
      this.checkIsFavorite(movieId);
      alert(res['message']);
    });
  }

  doOnmouseover(i: number) {
    this.realRate = this.rate;
    this.rate = i;
  }

  doOnMouseMove() {
    this.rate = this.realRate;
  }

  lockRate(rate: number) {
    const uid = localStorage.getItem('uid');
    if (uid === null) {
      alert('请先登录');
      return;
    }

    this.realRate = rate;
    this.rate = rate;
    this.http.get('/user/rate/' + this.movie['id'] + '/' + rate).subscribe(res => {
      alert(res['message']);
    });
  }

  submitComment() {
    if (this.commentSubject.title.length === 0 || this.commentSubject.content.length === 0) {
      alert('标题或者评论内容不能为空');
      return;
    }
    if (this.commentSubject.title.length > 64) {
      alert('标题过长');
      return;
    }
    if (this.commentSubject.content.length > 1024) {
      alert('评论内容过长');
      return;
    }
    this.http.post('/movie/comment/submit', this.commentSubject)
      .subscribe(res => {
        alert(res['message']);
        if (res['success']) {
          this.http.get(`/movie/comment/${this.movie['id']}/list?size=5`).subscribe(data => this.comments = data);
          this.commentSubject = new CommentSubject();
        }
      }, error => {
        alert(error.error['message']);
      });
  }

  changeCommentRate(rate: any) {
    this.commentSubject.rate = rate;
  }
}
